package de.htwds.mada.todolistDB.db;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import de.htwds.mada.todolistDB.model.Category;
import de.htwds.mada.todolistDB.model.Priority;
import de.htwds.mada.todolistDB.model.Todo;
import de.htwds.mada.todolistDB.model.TodoCategory;

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
	private static final String DATABASE_NAME = "Todos.db";
	private static final int DATABASE_VERSION = 1;
	private Dao<Priority, Integer> priorityDao = null;
	private Dao<Category, Integer> categoryDao = null;
	private Dao<Todo, Integer> todoDao = null;
	private Dao<TodoCategory, Integer> todocategoryDao = null;

	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
		try {
			Log.i(DatabaseHelper.class.getName(), "onCreate");
			TableUtils.createTable(connectionSource, Priority.class);
			TableUtils.createTable(connectionSource, Category.class);
			TableUtils.createTable(connectionSource, Todo.class);
			TableUtils.createTable(connectionSource, TodoCategory.class);
		} catch (SQLException e) {
			Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
			throw new RuntimeException(e);
		}

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,
			int oldVersion, int newVersion) {
		try {
			Log.i(DatabaseHelper.class.getName(), "onUpgrade");
			TableUtils.dropTable(connectionSource, Priority.class, true);
			TableUtils.dropTable(connectionSource, Category.class, true);
			TableUtils.dropTable(connectionSource, Todo.class, true);
			TableUtils.dropTable(connectionSource, TodoCategory.class, true);
			onCreate(db, connectionSource);
		} catch (SQLException e) {
			Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
			throw new RuntimeException(e);
		}

	}

	public Dao<Priority, Integer> getPriorityDao() throws SQLException {
		if (priorityDao == null) {
			priorityDao = getDao(Priority.class);
		}
		return priorityDao;
	}

	public Dao<Category, Integer> getCategoryDao() throws SQLException {
		if (categoryDao == null) {
			categoryDao = getDao(Category.class);
		}
		return categoryDao;
	}

	public Dao<Todo, Integer> getTodoDao() throws SQLException {
		if (todoDao == null) {
			todoDao = getDao(Todo.class);
		}
		return todoDao;
	}

	public Dao<TodoCategory, Integer> getTodocategoryDao() throws SQLException {
		if (todocategoryDao == null) {
			todocategoryDao = getDao(TodoCategory.class);
		}
		return todocategoryDao;
	}

	@Override
	public void close() {
		super.close();
		priorityDao = null;
		categoryDao = null;
		todoDao = null;
		todocategoryDao = null;
	}
}
